<?php
/**
 * Created by PhpStorm.
 * User: PGF
 * Date: 2017/4/20
 * Time: 23:11
 */

namespace GFPHP\Model;


use GFPHP\DataObject;
use GFPHP\Model;

/**
 * 用户授权,自动完成账号密码验证,以及登陆验证
 * Class Authorize
 * @package GFPHP\Model
 */
abstract class Authorize extends Model
{
    /**
     * 账户名字段,用于登陆校验,可是多个字段
     * @var array
     */
    protected $account_field = ['user_name'];

    /**
     * 账户密码,用于校验以及生产HASH密码
     * @var string
     */
    protected $password_field = 'user_password';

    /**
     * 登陆存储TOKEN类型
     * @var string  SESSION,COOKIE
     */
    protected $token_type = 'SESSION';

    /**
     * 登陆校验的TOKEN名称
     * @var string
     */
    protected $token_name = 'MEMBER_TOKEN';

    /**
     * 有效时间
     * @var int
     */
    protected $expire = 60*60*24*30;

    /**
     * 重写保存
     * @param array $data
     * @param bool  $primary_key
     * @return bool|int|array
     */
    public function save($data, $primary_key = false)
    {
        if (isset($data[$this->password_field]))
            $data[$this->password_field] = password_hash($data[$this->password_field]);
        return parent::save($data, $primary_key); // TODO: Change the autogenerated stub
    }

    /**
     * 登陆验证
     * @param string $account
     * @param string $password
     * @return bool|DataObject|string
     */
    public function login($account, $password)
    {
        $field_counter = 0;
        foreach ($this->account_field as $field) {
            if ($field_counter == 0)
                $this->where($field, $account);
            else
                $this->orWhere($field, $account);
            $field_counter++;
        }
        $account = $this->getOne();
        $hash_password = $account[$this->password_field];
        if(password_verify($password,$hash_password)){
            $token = $account.' || '.md5($hash_password);
            if ($this->token_type == 'SESSION')
                $_SESSION[$this->token_name] = $token;
            else
                setcookie($this->token_name,$token,$this->expire,'/');
            return $account;
        }else{
            return false;
        }
    }

    /**
     * 登陆效验,成功返回用户信息
     * @return array|DataObject|bool
     */
    public function checkLogin()
    {
        if ($this->token_type == 'SESSION')
            $token = SESSION($this->token_name);
        else
            $token = COOKIE($this->token_name);
        if (!$token)
            return false;
        $token = explode(' || ', base64_decode($token));
        if (count($token) != 2) {
            return false;
        }
        $field_counter = 0;
        foreach ($this->account_field as $field) {
            if ($field_counter == 0)
                $this->where($field, $token[0]);
            else
                $this->orWhere($field, $token[0]);
            $field_counter++;
        }
        $account = $this->getOne();
        if ($account) {
            $hash_password = md5($account[$this->password_field]);
            if ($hash_password == $token[1]) {
                return $account;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}